package com.aos.eap.base;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.KeyEvent;

import com.aos.eap.bean.net.BaseResponseBean;
import com.aos.eap.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseActivity;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;

/**
 * @author HX
 * @date 2019/12/5
 * @desc
 */
public class BaseDialogActivity extends FreeUI_BaseActivity {
    private transient FreeUI_GeneralFragmentDialog tipDialog;

    @Override
    protected int getLayoutResId() {
        return 0;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return null;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 当按下返回键时所执行的命令
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            showBackDialog(this);
            return false;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }
    protected void uploadLog(Throwable e){
        JsonObject map = new JsonObject();
        map.addProperty("Content", Log.getStackTraceString(e));
        map.addProperty("MACADDR", Constant.MAC_ADDR);
        NetUtils.requestGetNet(this, "EMS_PostLog", map, new FreeUI_EntityCallBack<BaseResponseBean<?>>(
                new TypeToken<BaseResponseBean<?>>() {
                }.getType()) {
            @Override
            public void onSuccess(Response<BaseResponseBean<?>> response) {
            }

            @Override
            public void onError(Response<BaseResponseBean<?>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_PostLog");
            }
        });
    }

    protected void showBackDialog(Context context) {
        tipDialog = FreeApi_DialogUtils.getConfirmAndCancelDialog(
                this, "确定要退出当前界面吗？", ((Activity) context)::finish, () -> {

                });
        tipDialog.show(getSupportFragmentManager(), "DIALOG");
    }
}
